package cn.edu.scau.cmi.wujiaming.experiment1;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;

import javax.swing.*;

public class Client extends Application {
	
	public static void main(String[] args) {
		Application.launch(args);
	}
	
	private GridPane getPane() {
		GridPane pane = new GridPane();
		pane.setAlignment(Pos.CENTER);
		pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
		pane.setHgap(5.5);
		pane.setVgap(5.5);

		TextField t1 = new TextField();
		TextField t2 = new TextField();
		// 接收主席输入
		pane.add(new Label("President:"), 0, 0);
		pane.add(t1, 1, 0);
		// 接收元帅输入
		pane.add(new Label("MarShell:"), 0, 1);
		pane.add(t2, 1, 1);
		Button btAdd = new Button("Add");
		pane.add(btAdd, 1, 2);
		GridPane.setHalignment(btAdd, HPos.RIGHT);

		btAdd.setOnAction(e -> {
			btAddActionHandler(t1, t2);
		});
		return pane;
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		GridPane pane = getPane();
		
		Scene scene = new Scene(pane);
		primaryStage.setTitle("Singleton");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	private void btAddActionHandler(TextField t1, TextField t2) {
		President president = President.getInstance(t1.getText());
		if (president == null) {
			JOptionPane.showMessageDialog(null, "主席只能有一个！");
		} else {
			JOptionPane.showMessageDialog(null, president.getName());
		}
		MarShell marShell = MarShell.getInstance(t2.getText());
		if (marShell == null) {
			JOptionPane.showMessageDialog(null, "元帅只能有十个！");
		} else {
			JOptionPane.showMessageDialog(null, marShell.getName());
		}
	}
	
	/**
	 * 用于综合性实验打开界面
	 */
	public void openNewView_singleton() {
		Stage stage = new Stage();
		Pane pane = getPane();
		stage.setScene(new Scene(pane));
		stage.setTitle("实验一-单例模式");
		stage.show();
	}
	
	public void openNewView_multiple() {
		Stage stage = new Stage();
		Pane pane = getPane();
		stage.setScene(new Scene(pane));
		stage.setTitle("实验一-多例模式");
		stage.show();
	}
}
